package com.xiaotou.listener;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;



public class EnvContextListener implements ServletContextListener{

	static Log logger = LogFactory.getLog(EnvContextListener.class);
	

	public void contextInitialized(ServletContextEvent ctxEvent) {
		Properties props  =   new  Properties();
	 	InputStream inputStream  =   this.getClass().getClassLoader().getResourceAsStream("env.properties");    
	 	try {
			props.load(inputStream);
			EnvParameter.setProps(props);
		} catch (IOException e) {
			logger.error(""+e);
		}
	 	
		final String webInfoPath = ctxEvent.getServletContext().getRealPath("/WEB-INF");
		System.setProperty("xiaotou.webinf.path", webInfoPath);
		System.setProperty("xiaotou.ruleparam.properties.path", webInfoPath+"/ruleparam.properties");
    }


	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		logger.info("EnvContextListener shut down");
	}

//	private void startWeiboPostManager() {
//		System.setProperty("weibo4j.oauth.consumerKey", EnvParameter.getProp(TConstant.SINAWEIBOCONSUMERKEY));
//    	System.setProperty("weibo4j.oauth.consumerSecret", EnvParameter.getProp(TConstant.SINAWEIBOCONSUMERSECRET));
//		ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
//		executorService.scheduleWithFixedDelay(new WbPostManger(), 10, 30,
//                TimeUnit.SECONDS);
//	}
	
}
